package com.example.algorithm.tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author : zhy
 * @since : 2022/8/8
 * 计算完全二叉树的节点数，这道题目主要考察完全二叉树的概念
 */
public class CompletelyTree {

    public int countNodes(TreeNode root) {
        if(root==null){
            return 0;
        }
        int count=0;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty())
        {
            int size = queue.size();
            while(size-->0){
                TreeNode poll = queue.poll();
                count++;
                if(poll.left!=null){
                    queue.offer(poll.left);
                }
                if(poll.right!=null){
                    queue.offer(poll.right);
                }
            }
        }
        return count;
    }
}
